package display;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.HashMap;

import service.CDManager;
import domain.CD;

import service.TrackManager;
import domain.Track;
//import service.CDManager;
//import domain.Grupo;

@Controller
@RequestMapping("/cd")
public class CDController {
 
 @Autowired
 private CDManager cdManager;
 
  @Autowired
 private TrackManager trackManager;
 //@Autowired
 //private CDMnager CDManager;
 
 @RequestMapping(value="/listado", method = RequestMethod.GET)
 public ModelAndView listado() {

   Map<String, Object> myModel = new HashMap<String, Object>();
   myModel.put("CDs", this.cdManager.getAll());
   return new ModelAndView("listaCDs", "model", myModel);

 }




 
 @RequestMapping(value="/detalleCD/{pk_CD}", method = RequestMethod.GET)
 public ModelAndView detalleCD(@PathVariable("pk_CD") String id) {

   ModelAndView modelAndView = new ModelAndView();
   CD element = this.cdManager.getById(id);

   if (element == null){
     System.out.println("NULO");
	}else{ 
    modelAndView = new ModelAndView("detalleCD");
    modelAndView.addObject("CD", element);

    /*Map<String, Object> myModel = new HashMap<String, Object>();
    if(this.trackManager.getAllByCDID(id) != null){
      myModel.put("tracks", this.trackManager.getAllByCDID(id));
    }
    modelAndView.addObject("model", myModel);*/
		//Map<String, Object> myModel = new HashMap<String, Object>();
    //myModel.put("CDs", this.cdManager.getAllByBandaID(id));
		//modelAndView.addObject("model", myModel);
   }
    return modelAndView;	 
 }

 
 @RequestMapping(value="/actualizarCD/{pk_CD}", params = { "pk_CD","nombre","anoPublicacion","precio","fk_banda" }, method = RequestMethod.GET)
 public String actualizarCD(
     @PathVariable("pk_CD") String id, 
     @RequestParam("nombre") String nombre,
     @RequestParam("anoPublicacion") String anoPublicacion,
     @RequestParam("precio") String precio,
     @RequestParam("fk_banda") String idBanda
     
  ) {
   CD element = this.cdManager.getById(id);
   System.out.println("id: "+ id);
   element.setNombre(nombre);
   element.setAnoPublicacion(anoPublicacion);
   element.setPrecio(precio);
   element.setFk_banda(idBanda);   
   return "forward:/cd/listado";
 }
 
 @RequestMapping(value="/agregarCD", method = RequestMethod.GET)
 public ModelAndView aregar() {
 	return new ModelAndView("agregarCD");
 }
 
 @RequestMapping(value="/insertarCD", params = { "nombre","anoPublicacion","precio","fk_banda"}, method = RequestMethod.GET)
 public String actualizar(
     @RequestParam("nombre") String nombre,
     @RequestParam("anoPublicacion") String anoPublicacion,
     @RequestParam("precio") String precio,
     @RequestParam("fk_banda") String fk_banda
  ) {
   CD element = new CD();
   element.setNombre(nombre);
   element.setAnoPublicacion(anoPublicacion);
   element.setPrecio(precio);
   element.setFk_banda(fk_banda);
   this.cdManager.insert(element);
   return "forward:/cd/listado";
 }
 
  @RequestMapping(value="/eliminarCD/{pk_CD}", method = RequestMethod.GET)
 public String eliminar(@PathVariable("pk_CD") String id) {
   if (id != null){
	   this.cdManager.delete(id);
   }
   return "forward:/cd/listado";
 } 

 public void setCDManager(CDManager cdManager) {
   this.cdManager = cdManager;
 } 
}